home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Format CD 40
/
Amiga Format CD40 (1999-05-11)(Future Publishing)(GB)(Track 1 of 3)[!][issue 1999-06].iso
/
-readerstuff-
/
iain_hamilton
/
density.c
< prev
next >
Wrap
C/C++ Source or Header
|
1999-03-27
|
6KB
|
269 lines
/* Density Plotter
Written by Iain Hamilton
20th November 1998
compiled for 030 on VBCC
iain@slarti.demon.co.uk
http://www.slarti.demon.co.uk
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void densplot();
void clrscr();
main(void)
{
int exitcode = 0;
int choice;
/* menu code starts here */
while (exitcode == 0)
{
clrscr();
printf("\n\t\tPerform a density plot for a given equation\n");
printf("\n\t\tPlease Choose an option from the menu..\n\n");
printf("\t\t1......y^2+3x+x^3\n\n");
printf("\t\t2......sin x sin y\n\n");
printf("\t\t3......y/((x^2+y^2)^1.5)\n\n");
printf("\t\t4......x^3+y^3-2x\n\n");
printf("\t\t5......cos x + sin y(x^2+y)^1.5\n\n");
printf("\t\t6......Exit the Program\n\n");
printf("\t\tPlease make your choice with the appropriate key : ");
/* Read the Keypress */
choice = getch();
clrscr();
switch (choice)
{
case '1' : densplot(choice);
break;
case '2' : densplot(choice);
break;
case '3' : densplot(choice);
break;
case '4' : densplot(choice);
break;
case '5' : densplot(choice);
break;
case '6' : exitcode = 1;
break;
default : clrscr();
printf("\nChoice out of range, please try again...");
break;
}
printf("\n\t\tPress a key to continue....\n");
getch();
}
}
/* start of routine to calculate and plot density equations */
void densplot(choice)
{
/* declare variables for use in procedure */
float x;
float y;
int counter = 1;
float answer;
float highest;
float lowest;
float stepper;
float xlow;
float xhigh;
float ylow;
float yhigh;
float xstep;
float ystep;
clrscr();
/* check which equation is to be run and set the x&y ranges for the for loops */
if (choice == 49)
{
xlow=-2.0;
xhigh=2.05;
ylow=-2.0;
yhigh=2.05;
xstep=0.1;
ystep=0.1;
}
else if (choice == 50)
{
xlow=0;
xhigh=3.14;
ylow=0;
yhigh=3.14;
xstep=0.1;
ystep=0.1;
}
else if (choice == 51)
{
xlow=1.0;
xhigh=3.05;
ylow=1.0;
yhigh=5.05;
xstep=0.1;
ystep=0.1;
}
else if (choice == 52)
{
xlow=0;
xhigh=4.05;
ylow=1.0;
yhigh=6.05;
xstep=0.1;
ystep=0.1;
}
else if (choice == 53)
{
xlow=-3;
xhigh=0.05;
ylow=-3;
yhigh=0.05;
xstep=0.1;
ystep=0.1;
}
/* begin the for loops with the set x&y values */
for (x=xlow; x<=xhigh; x=x+xstep)
{
for (y=ylow; y<=yhigh; y=y+ystep)
{
/* calculate an equation depending on users selection */
if (choice == 49)
{
answer = (y*y)+(3*x)+(x*x*x);
}
else if (choice == 50)
{
answer = sin(x)*sin(y);
}
else if (choice == 51)
{
answer = y/pow(((x*x)+(y*y)),1.5);
}
else if (choice == 52)
{
answer = (x*x*x)+(y*y*y)-(2*x);
}
else if (choice == 53)
{
answer = cos(x)+ sin(y)*pow(((x*x)+y),1.5);
}
/* get start values for lowest and highest */
if (counter == 1)
{
lowest = answer;
highest = answer;
counter++;
}
/* collect data for highest and lowest */
if (answer < lowest)
{
lowest = answer;
}
if (answer > highest)
{
highest = answer;
}
}
}
/* find the difference for highest & lowest and use this to create 7 sections */
stepper = (highest - lowest)/7;
/* begin the density plot */
for (x=xlow; x<=xhigh; x=x+xstep)
{
/* start a new line following x increment */
printf("\n ");
for (y=ylow; y<=yhigh; y=y+ystep)
{
/* select equation to be run */
if (choice == 49)
{
answer = (y*y)+(3*x)+(x*x*x);
}
else if (choice == 50)
{
answer = sin(x)*sin(y);
}
else if (choice == 51)
{
answer = y/pow(((x*x)+(y*y)),1.5);
}
else if (choice == 52)
{
answer = (x*x*x)+(y*y*y)+(2*x);
}
else if (choice == 53)
{
answer = cos(x)+sin(y)*pow(((x*x)+y),1.5);
}
/* check which section the answer is in and print a character accordingly */
if (answer <= (lowest+stepper))
{
printf(".");
}
else if (answer > (lowest+stepper) && answer <= (lowest+(stepper*2)))
{
printf(",");
}
else if (answer > (lowest+(stepper*2)) && answer <= (lowest+(stepper*3)))
{
printf(";");
}
else if (answer > (lowest+(stepper*3)) && answer <= (lowest+(stepper*4)))
{
printf("-");
}
else if (answer > (lowest+(stepper*4)) && answer <= (lowest+(stepper*5)))
{
printf("+");
}
else if (answer > (lowest+(stepper*5)) && answer <= (lowest+(stepper*6)))
{
printf("#");
}
else if (answer > (lowest+(stepper*6)) && answer <= highest)
{
printf("*");
}
}
}
}
void clrscr()
{
/* this required for compile on amiga, can be removed for Turbo C/++ on the PC */
printf("\f");
}